/*
  Custom effect that adapts the fireworks effect so each burst is a rainbow
  of colors

  Keith Lord - 2018

  LICENSE

  The MIT License (MIT)

  Copyright (c) 2018  Keith Lord 

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  
  CHANGELOG
  2018-11-13 initial version
*/

#ifndef RainbowFireworks_h
#define RainbowFireworks_h

#include "../WS2812FX.h"

extern WS2812FX ws2812fx;

uint16_t rainbowFireworks(void) {
  WS2812FX::Segment* seg = ws2812fx.getSegment(); // get the current segment
  uint16_t seglen = seg->stop - seg->start + 1;

  ws2812fx.setBrightness(255); // force full brightness
  ws2812fx.fade_out();

  for(uint16_t i=seg->start + 6; i <= seg->stop - 6; i++) {
    uint32_t color = ws2812fx.getPixelColor(i);

    if(color == 0x7F0000) {
      ws2812fx.setPixelColor(i-1, 0xFF7F00); // orange
      ws2812fx.setPixelColor(i+1, 0xFF7F00);
    }
    if(color == 0x3F0000) {
      ws2812fx.setPixelColor(i-2, 0xFFFF00); // yellow
      ws2812fx.setPixelColor(i+2, 0xFFFF00);
    }
    if(color == 0x1F0000) {
      ws2812fx.setPixelColor(i-3, 0x00FF00); // green
      ws2812fx.setPixelColor(i+3, 0x00FF00);
    }
    if(color == 0x0F0000) {
      ws2812fx.setPixelColor(i-4, 0x0000FF); // blue
      ws2812fx.setPixelColor(i+4, 0x0000FF);
    }
    if(color == 0x070000) {
      ws2812fx.setPixelColor(i-5, 0x4B0082); // indigo
      ws2812fx.setPixelColor(i+5, 0x4B0082);
    }
    if(color == 0x030000) {
      ws2812fx.setPixelColor(i-6, 0x9400D3); // violet
      ws2812fx.setPixelColor(i+6, 0x9400D3);
    }
  }

  // randomly create a red pixel
  for(uint16_t i=0; i<max(1, seglen/20); i++) {
    if(ws2812fx.random8(10) == 0) {
      uint16_t index = seg->start + 6 + ws2812fx.random16(seglen - 12);
      ws2812fx.setPixelColor(index, RED);
      ws2812fx.setCycle();
    }
  }

  return(seg->speed / seglen);
}
#endif
